from tool.es_clent import connect_es, store_data_bulk
import json
from tool.string_tool import clean_des
from tool.es_clent import create_index

def create_api_index(es_object, index_name):
    """
    根据../data/es_index/apis.json创建apis index
    :return:
    """
    created = False
    settings = json.load(open("../data/es_index/apis.json"))
    try:
        if not es_object.indices.exists(index_name):
            es_object.indices.create(index=index_name, ignore=400, body=settings)
            print("create index success")
        created = True
    except Exception as ex:
        print(str(ex))
    finally:
        return created


def process_apis():
    """
    读取../data中的apis.json数据,批量插入es库中
    :return:
    """
    api_datas = json.load(open("../data/apis.json"))
    insert_data = []
    for api in api_datas:
        clean_api = {
            "api_id": api["url"][36:],
            "category": api["category"],
            "description": clean_des(api["description"]) if api["description"] else None,
            "developer_ids": [developer["developer_link"][9:] for developer in api["developers"]] if api["developers"] else None,
            "followers_num": len(api["followers"]) if api["followers"] else 0,
            "name": api["name"],
            "status": 1 if api["status"] and api["status"][-1]["status"] in ("Recommended ", "Active", "Recommended") else 0,
        }
        insert_data.append(clean_api)
    store_data_bulk(es_object=es, index_name="apis", doc_type="apis", data_list=insert_data)


if __name__ == "__main__":
    es = connect_es()
    es.indices.delete(index="apis")
    create_index(es, "apis", "../data/es_index/apis.json")
    process_apis()
